/**
 * Created by Session on 16/1/9.
 */
function initContainer() {

  $('#risk-count').on('click', function () {
    var scrollHeight = $('#risk-items').offset().top
    $('.report-container').animate({scrollTop: scrollHeight}, 300)
  })

  //点击查看详情
  var id = ''
  $('.check-table').on('click', '.table-href', function () {
    $('.a-detail').hide()
    $(this).parents('ul').find('.a-detail').show(100)
    $('.msk-detail').show().css('right', (480 - ($(window).width() - 900) / 2) + 'px')
  })

  $('.report-container').on('click', function (e) {
    e = e || window.event
    e.stopPropagation()
    e.preventDefault()


  })

  $('.container').on('click', function (e) {
    e = e || window.event
    e.stopPropagation()
    e.preventDefault()
  })


  //id点击
  var display = 0
  $('a.jz').click(function () {
    if (display == 0) {
      $('.j-rpi-toggle-target').show(400)
      display = 1
    } else if (display == 1) {
      $('.j-rpi-toggle-target').hide(400)
      display = 0
    }
  })

  if ($('#canvas-main').length > 0) {
    var canvasMain = $('#canvas-main')[0]
    var mainCtx = canvasMain.getContext('2d')
    var score = parseInt($('#result-score').text(), 10)
    var resultCat = $('#result-cat').text()
    var i = 1
    if (score > 0 && score < 100) {
      i = (100 - score) / 100
    }
    if (score === 0) {
      i = 1
    }
    // console.log(i);
    mainCtx.rotate(-Math.PI / 2)
    mainCtx.lineWidth = 8
    // console.log(resultCat);
    if (resultCat.indexOf('拒绝') > -1) {
      $('#result-cat').addClass('reject')
      mainCtx.strokeStyle = '#ff6c5c'
    } else if (resultCat.indexOf('通过') > -1) {
      $('#result-cat').addClass('accept')
      mainCtx.strokeStyle = '#8cdb65'
    } else if (resultCat.indexOf('审核') > -1) {
      $('#result-cat').addClass('review')
      mainCtx.strokeStyle = '#f8d436'
    }
    mainCtx.beginPath()
    mainCtx.arc(-75, 75, 69, 0, Math.PI * (i * 2), true)
    mainCtx.stroke()
    // 75, 75, 0
    // 17, 103, 0.1
    // -17, 104

    var canvasBg = $('#canvas-bg')[0]
    var bgCtx = canvasBg.getContext('2d')
    bgCtx.fillStyle = '#fafafa'
    bgCtx.beginPath()
    bgCtx.arc(61, 61, 52, 0, Math.PI * 2, true)
    bgCtx.closePath()
    bgCtx.fill()
  }

  // 多头详情点击下拉查看
  $('.dimension-title').on('click', function () {
    var dimensionList = $(this).next('.dimension-list')
    if (dimensionList.length) {
      dimensionList.slideToggle(200)
    }
  })

  $('.risk-detail-title').on('click', function () {
    var detailList = $(this).next('.risk-detail-list')
    if (detailList.length) {
      detailList.slideToggle(200)
    }
    //数据库中会出现这个※,换成*,主要原因太丑了
    var li = $(this).next('.risk-detail-list').find('.risk-detail-sub-list').children('li')
    $.each(li, function (index, value) {
      var text = $(value).text().replace(/[※]/g, '*')
      $(value).text(text)
    })
  })

  //提示报告中是否存在命中未分组的规则情况
  if ($('#tip-value').val() == 'true') {
    $('.template-tip').css({'display': 'block'})
  }

  //新图表的展示
  var policyDataVal = $('#policy_info').val()
  var radarDataVal = $('#risk-radar-hidden').val()//老报告和没有开启权重，undefined
  var policyData = policyDataVal ? JSON.parse(policyDataVal) : ''
  var radarData = radarDataVal ? JSON.parse(radarDataVal) : ''
  //仪表盘
  if (policyDataVal != undefined) {
    setTimeout(function () {
      initGaugeGraph(policyData)
    }, 200)
    //雷达图
    if (radarDataVal != undefined) {
      setTimeout(function () {
        initRadarGraph(radarData)
      }, 200)
    }
  }

  function initRadarGraph(testData) {
    var radarDom = document.getElementById('radar')
    var radarChart = echarts.init(radarDom)
    var arr = []
    $.each(testData, function (i, item) {
      arr.push(item.score)
    })

    var option = {
      radar: [
        {
          indicator: (function () {
            var res = []
            $.each(testData, function (i, item) {
              if (item.group_name.length > 9) {
                item.group_name = item.group_name.substr(0, 9) + '...'
              }
              res.push({text: item.group_name, max: 100, min: 0})
            })
            return res
          })(),
          center: ['50%', '50%'],
          radius: '65%',
          startAngle: 90,
          splitNumber: 4,
          name: {
            formatter: '{value}',
            padding: [0, 10, 0, 0],
            textStyle: {
              fontFamily: 'PingFangSC-Regular',
              fontSize: 12,
              color: ' #4A4A4A',
            },
          },
          splitArea: {
            show: false,
          },
          splitLine: {
            lineStyle: {
              color: ['#E3E8F1'],
            },
          },
          axisLine: {
            lineStyle: {
              color: '#E3E8F1',
            },
          },
        },
      ],
      series: [
        {
          name: '雷达图',
          type: 'radar',
          symbol: 'disc',
          data: [
            {
              value: arr,
              lineStyle: {
                color: '#F41313',
                // width: 1,
                opacity: 0.36,
              },
            },
          ],
        },
      ],
    }
    radarChart.setOption(option)
  }

  function initGaugeGraph(policyData) {
    var score = $('#policyScore').val()
    var flag1 = policyData.review_threshold / 100
    var flag2 = policyData.deny_threshold / 100
    var gaugeDom = $('.new-graph .gauge-graph')
    var myChart = echarts.init(gaugeDom[0])
    var option = {
      series: [
        {
          name: '业务指标',
          type: 'gauge',
          radius: 120,
          axisLine: {            // 坐标轴线
            lineStyle: {       // 属性lineStyle控制线条样式
              width: 30,
              color: [[flag1, '#8cdb65'], [flag2, '#f8d438'], [1, '#ff6c5c']],
            },
          },
          splitLine: {
            lineStyle: {
              opacity: 0.75,
            },
          },
          axisLabel: {
            color: '#BDBDBD',
          },
          pointer: {
            show: true,
            length: '60%',
          },
          itemStyle: {
            borderWidth: 30,
          },
          detail: {
            formatter: '{value}',
            fontSize: 40,
            offsetCenter: [0, '60%'],
          },
          data: [{value: score}],
        },
      ],
    }
    myChart.setOption(option)
  }
}
